#include <stdio.h>
#include <stdlib.h>
#include "commom.h"

void queue_init(Queue_t *pQueue)
{
	int i = 0;

	if(NULL == pQueue)
	{
		return;
	}

	pQueue->front = pQueue->rear = 0;
	for (i=0; i<QUEUE_MAX_SIZE; i++)
	{
		pQueue->data[i] = NULL;
	}
}

void queue_destory(Queue_t *pQueue)
{
	int i = 0;
	
	if(NULL == pQueue)
	{
		return;
	}
	
	pQueue->front = pQueue->rear = 0;
	for (i=0; i<QUEUE_MAX_SIZE; i++)
	{
		if(NULL != pQueue->data[i])
		{
			free(pQueue->data[i]);
		}
	}
}

int queue_en(Queue_t *pQueue, char *pVal)
{
	int len = 0;

	if(NULL == pQueue || NULL == pVal)
	{
		return -1;
	}

	if (queue_full(pQueue) == 1)
	{
		printf("queue_en:Queue full\n");
		return -1;
	}

	if (queue_find(pQueue, pVal) == 1)
	{
		//printf("queue_en:allready exist:%s\n", pVal);
		return -1;
	}

	if(NULL != pQueue->data[pQueue->rear])
	{
		free(pQueue->data[pQueue->rear]);
	}

	len = strlen(pVal) + 1;
	pQueue->data[pQueue->rear] = (char *)malloc(len);
	if(NULL == pQueue->data[pQueue->rear])
	{
		printf("queue_en:malloc fail\n");
		return -1;
	}
	memset(pQueue->data[pQueue->rear], 0x00, len);
	strcpy(pQueue->data[pQueue->rear], pVal);
	printf("URL=%s\n", pVal);

	pQueue->rear = (pQueue->rear + 1)%QUEUE_MAX_SIZE;

	return 0;
}

int queue_de(Queue_t *pQueue, char *pVal)
{
	int len;

	if (NULL == pQueue || NULL == pVal)
	{
		printf("queue_de:NULL pointer\n");
		return -1;
	}

	if (queue_empty(pQueue) == 1)
	{
		printf("queue_de:queue empty\n");
		return -2;
	}

	len = strlen(pQueue->data[pQueue->front]);
	if(len <= 0 || len > URL_MAX_LEN)
	{
		printf("queue_de:out of length\n");
		return -3;
	}

	strcpy(pVal, pQueue->data[pQueue->front]);

	free(pQueue->data[pQueue->front]);
	pQueue->data[pQueue->front] = NULL;

	pQueue->front = (pQueue->front + 1)%QUEUE_MAX_SIZE;

	return 0;
}

int queue_empty(Queue_t *pQueue)
{
	if (NULL == pQueue)
	{
		printf("queue_empty:NULL pointer\n");
		return -1;
	}

	if (pQueue->front == pQueue->rear)
	{
		return 1;
	}

	return 0;
}

int queue_full(Queue_t *pQueue)
{
	if (NULL == pQueue)
	{
		printf("queue_full:NULL pointer\n");
		return -1;
	}
	
	if (pQueue->front == (pQueue->rear + 1)%QUEUE_MAX_SIZE)
	{
		return 1;
	}
	
	return 0;
}

int queue_len(Queue_t *pQueue)
{
	if (NULL == pQueue)
	{
		printf("queue_len:NULL pointer\n");
		return -1;
	}
	
	return (pQueue->rear + QUEUE_MAX_SIZE - pQueue->front) % QUEUE_MAX_SIZE;
}

int queue_find(Queue_t *pQueue, char *pVal)
{
	int len = 0, i = 0;

	if (NULL == pQueue || NULL == pVal)
	{
		printf("queue_find:NULL pointer\n");
		return -1;
	}
	
	len = queue_len(pQueue);
	for (i=0; i<len; i++)
	{
		if (strcmp(pQueue->data[(pQueue->front + i) % QUEUE_MAX_SIZE], pVal) == 0)
		{
			return 1;
		}
	}

	return 0;
}

void lcs(const char *s1, const char *s2, char *plcs)
{
	if(NULL == s1 || NULL == s2 || NULL == plcs)
	{
		printf("lcs:NULL pointer\n");
		return;
	}
	
	if(strlen(s1) == 0 || strlen(s2) == 0)
	{
		printf("lcs:str len is 0\n");
		return;
	}

	if(s1[0] == s2[0])
	{
		int len = strlen(plcs);
		if(len >= LCS_STR_LEN)
		{
			printf("lcs:reach max lcs len, will stop\n");
			return;
		}
		plcs[len] = s1[0];
		lcs(s1+1, s2+1, plcs);
	}
	else
	{
		char lcs_tmp[3][LCS_STR_LEN+1] = {{0x00}, {0x00}, {0x00}};
		int index = 0;

		memset(&lcs_tmp, 0x00, sizeof(lcs_tmp));
		lcs(s1+1, s2, &lcs_tmp[0]);
		lcs(s1, s2+1, &lcs_tmp[1]);
		lcs(s1+1, s2+1, &lcs_tmp[2]);

		if(strlen(&lcs_tmp[1]) > strlen(&lcs_tmp[index]))
		{
			index = 1;
		}
		if(strlen(&lcs_tmp[2]) > strlen(&lcs_tmp[index]))
		{
			index = 2;
		}

		if(strlen(plcs) < strlen(&lcs_tmp[index]))
		{
			strcpy(plcs, &lcs_tmp[index]);
		}
	}
}
